﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;

namespace Xceed.Silverlight.Data
{
  internal sealed class DataSourceFilterContext
  {
    #region Constructors

    internal DataSourceFilterContext(
      Predicate<object> filter,
      FilterExpressionBase filterExpression )
    {
      m_filter = filter;

      if( filterExpression != null )
      {
        m_filterExpression = filterExpression.Clone();
      }
    }

    internal DataSourceFilterContext( FilteringHandler handler )
      : this( handler.Filter, handler.FilterExpression )
    {
    }

    private DataSourceFilterContext( DataSourceFilterContext copy )
      : this( copy.Filter, copy.FilterExpression )
    {
    }

    #endregion

    #region Filter Property

    internal Predicate<object> Filter
    {
      get
      {
        return m_filter;
      }
    }

    private readonly Predicate<object> m_filter;

    #endregion

    #region FilterExpression Property

    internal FilterExpressionBase FilterExpression
    {
      get
      {
        return m_filterExpression;
      }
    }

    private readonly FilterExpressionBase m_filterExpression;

    #endregion

    public override int GetHashCode()
    {
      int hashCode = 11;

      hashCode += 17 * DataSourceFilterContext.GetHashCode( this.Filter );
      hashCode += 17 * DataSourceFilterContext.GetHashCode( this.FilterExpression );

      return hashCode;
    }

    public override bool Equals( object obj )
    {
      if( object.ReferenceEquals( obj, this ) )
        return true;

      DataSourceFilterContext context = obj as DataSourceFilterContext;

      if( object.ReferenceEquals( context, null ) )
        return false;

      if( context.GetHashCode() != this.GetHashCode() )
        return false;

      return ( object.Equals( context.Filter, this.Filter ) )
          && ( object.Equals( context.FilterExpression, this.FilterExpression ) );
    }

    internal DataSourceFilterContext Clone()
    {
      return new DataSourceFilterContext( this );
    }

    private static int GetHashCode( object obj )
    {
      if( object.ReferenceEquals( obj, null ) )
        return 0;

      return obj.GetHashCode();
    }
  }
}
